#!/usr/bin/env python
# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##-- G03 -- S00 ------------------------------------------------------------##
__all__ = []

from .. import pilelib as stack
from .. import quickplayscore as quickplay
from ..formlib import Layout
from ..gamelib import info as gi, game
from ..hint import CautiousDefaultHint

GT, SL = gi.GT, gi.SL
StackWrapper = stack.StackWrapper


class Needle(game.Game):
    """
    Finish cards on _foundation_ within a turn.

    _row_
      Pile 9 horz. Init deal [8, 8, 4, 2, 0, 2, 4, 8, 8] open card. Build down
      in rank by alt color. Play top.

    _reserve_
      Pile 1 NW. Init deal 8 open cards. Build any 18 cards. Play top.

    _foundation_
      Pile 4 horz NE. Build up in rank by same suit. Play top.
    ------
    :cvar Stack _talon_: stack.InitialDealTalon
    :cvar Stack _foundation_: stack.SS_Foundation
    :cvar Stack _row_: stack.AC_RowStack (max_accept=1)
    :cvar Stack _reserves_: stack.ReserveStack (max_cards=18)
    :cvar Hint _hint_: hint.CautiousDefaultHint
    """
    __info__ = dict(id=318, name='Needle', game_type=(GT.FREECELL |
                    GT.OPEN), decks=1, redeals=0, version='0.80fc',
                    skill_level=SL.MOSTLY_SKILL)
    __author__ = 'Skomoroh'
    __version__ = 7.70

    _talon_ = stack.InitialDealTalon
    _foundation_ = stack.SS_Foundation
    _row_ = StackWrapper(stack.AC_RowStack, max_move=1)
    _reserve_ = StackWrapper(stack.ReserveStack, max_cards=18)
    _hint_ = CautiousDefaultHint

    def _create_game(self, rows=9, **kw):
        l, s = Layout(self), self.s
        self.table_size = (l.XM + max(rows * l.XS,
                                      4 * l.XS + l._get_stack_width(18, l.XS)),
                            l.YM + l.YS + l._get_stack_height(12, l.YS),)
        x, y = l.XM, l.YM
        s['reserves'].append(self._reserve_(x, y, game=self,
                                         offset=(l.XOFFSET, 0)))
        dx = self.width - 4 * l.XS
        x = dx
        for suit in l.SUTIS:
            s['foundations'].append(self._foundation_(x, y, game=self,
                                                   suit=suit))
            x += l.XS
        x = (self.width - rows * l.XS) / 2
        y += l.YS
        self._set_region(s['reserves'], l._region(w=dx - l.HCW, h=y - l.HCH))
        for i in range(rows):
            s['rows'].append(self._row_(x, y, game=self))
            x += l.XS
        x, y = self.width - l.XS, self.height - l.YS
        s['talon'] = self._talon_(x, y, game=self)
        l.defaultStackGroups()
        return l

    def _start(self, deal=(4, 4, 3, 3, 2, 2, 2, 2), **kw):
        for i in range(8):
            self.s['talon'].dealRow(rows=self.s['reserves'], frames=0)
        for i in deal[:-4]:
            self.s['talon'].dealRow(rows=self.s['rows'][:i], frames=0)
            self.s['talon'].dealRow(rows=self.s['rows'][-i:], frames=0)
        for i in deal[-4:]:
            self.s['talon'].dealRow(rows=self.s['rows'][:2])
            self.s['talon'].dealRow(rows=self.s['rows'][-2:])

    _quickplay_ = quickplay.Needle


class Haystack(Needle):
    """
    Finish cards on _foundation_ within a turn.

    _row_
      Pile 9 horz. Init deal [8, 8, 4, 2, 0, 2, 4, 8, 8] open card. Build down
      in rank by alt color. Play top.

    _reserve_
      Pile 1 NW. Init deal 8 open cards. Build any 8 cards. Play top.

    _foundation_
      Pile 4 horz NE. Build up in rank by same suit. Play top.
    ------
    :cvar Stack _talon_: stack.InitialDealTalon
    :cvar Stack _foundation_: stack.SS_Foundation
    :cvar Stack _row_: stack.AC_RowStack (max_accept=1)
    :cvar Stack _reserves_: stack.ReserveStack (max_cards=8)
    :cvar Hint _hint_: hint.CautiousDefaultHint
    """
    __info__ = dict(id=319, name='Haystack', game_type=(GT.FREECELL |
                    GT.OPEN), decks=1, redeals=0, version='0.80fc',
                    skill_level=SL.MOSTLY_SKILL)
    __author__ = 'Skomoroh'
    __version__ = 7.70

    _reserve_ = StackWrapper(stack.ReserveStack, max_cards=8)


class Pitchfork(Needle):
    """
    Finish cards on _foundation_ within a turn.

    _row_
      Pile 9 horz. Init deal [8, 8, 4, 2, 0, 2, 4, 8, 8] open card. Build down
      in rank by alt color. Play top.

    _reserve_
      Pile 1 NW. Init deal 8 open cards. Build none. Play top.

    _foundation_
      Pile 4 horz NE. Build up in rank by same suit. Play top.
    ------
    :cvar Stack _talon_: stack.InitialDealTalon
    :cvar Stack _foundation_: stack.SS_Foundation
    :cvar Stack _row_: stack.AC_RowStack (max_accept=1)
    :cvar Stack _reserves_: stack.OpenStack
    :cvar Hint _hint_: hint.CautiousDefaultHint
    """
    __info__ = dict(id=367, name='Pitchfork', game_type=(GT.FREECELL |
                    GT.OPEN), decks=1, redeals=0, version='0.90fc',
                    skill_level=SL.MOSTLY_SKILL)
    __author__ = 'Skomoroh'
    __version__ = 7.70

    _reserve_ = stack.OpenStack


PROVIDES = [Needle, Haystack, Pitchfork]